import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;


public class JMTable extends CustomItem {
	
	private int largura;
	private int tam_fonte;
	private Vector colunas;
	private Vector linhas;
	private Font fonte;
	private Font fonteCols;
	private int altura;
	
	public JMTable(){
		super("Tabela");
	}
	
	public JMTable(Vector colunas, Vector dados, int tam_fonte, int largura){
		super("");
		this.largura = largura;
		this.colunas = colunas;
		this.linhas = dados;
		this.tam_fonte = tam_fonte;
		
		fonte = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, (tam_fonte == 0 ? Font.SIZE_SMALL : tam_fonte == 1 ? Font.SIZE_MEDIUM : Font.SIZE_LARGE));
		fonteCols = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, (tam_fonte == 0 ? Font.SIZE_SMALL : tam_fonte == 1 ? Font.SIZE_MEDIUM : Font.SIZE_LARGE));
		
		altura = fonte.getHeight() + (fonteCols.getHeight() * linhas.size());
	}
	
	protected int getMinContentHeight() {
		// TODO Auto-generated method stub
		return fonte.getHeight();
	}

	protected int getMinContentWidth() {
		// TODO Auto-generated method stub
		return largura;
	}

	protected int getPrefContentHeight(int arg0) {
		// TODO Auto-generated method stub
		return altura;
	}

	protected int getPrefContentWidth(int arg0) {
		// TODO Auto-generated method stub
		return largura;
	}

	protected void paint(Graphics g, int w, int h) {
		// TODO Auto-generated method stub
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, w - 1, h - 1);
		
		g.setColor(200, 200, 200);
		g.fillRect(0, 0, w, fonte.getHeight());
		g.setColor(0, 0, 0);
		g.drawLine(0, fonte.getHeight(), w, fonte.getHeight());
		try{
			for(int i = 1; i < colunas.size(); i++){
				g.drawLine((w/colunas.size()) * i, 0, (w/colunas.size()) * i, h - 1);
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("primeiro for...");
		}
		
		g.drawRect(0, 0, w - 1, h - 1);
		g.setStrokeStyle(g.DOTTED);
		//desenha linhas horizontais entre as linhas da tabela
		try{
			for(int i = 1; i < linhas.size(); i++){
				g.drawLine(0, fonteCols.getHeight() + (fonte.getHeight() * i), w, fonteCols.getHeight() + (fonte.getHeight() * i));
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Segundo for...");
		}
		
		g.setFont(fonteCols);
		try{
			for(int i = 1; i <= colunas.size(); i++){
				g.drawString(colunas.elementAt(i - 1).toString(), ((w/colunas.size()/2) * i + (w/colunas.size()/2) * (i - 1)), fonteCols.getHeight(), Graphics.HCENTER | Graphics.BOTTOM);
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Terceiro for...");
		}
		
		
		  g.setFont(  fonte  )  ; 
		  try{
			  for (int  i = 0; i < linhas.size(); i++ ) {   
//				  if  (  i == indice  )    
//					  g.setColor(0, 0, 255);    
//				  else if    (  i == indice_marc  )    
//					  g.setColor(255, 0, 0);   
//				  else     g.setColor(0, 0, 0);
				  g.setColor(0, 0, 0);
				  System.out.println("size linha: " + linhas.size());
				  System.out.println("size coluna: " + colunas.size());
				  String[] dados = (String[])linhas.elementAt(i);    
				  for    (int  j = 1; j <= colunas.size(); j++) {   
					  g.drawString(dados[j - 1], ((w / colunas.size()) / 2) * j + ( w / colunas.size() / 2 * (j - 1))  ,  fonteCols.getHeight()   +   (fonte.getHeight()   *   (i + 1))  , Graphics.HCENTER | Graphics.BOTTOM  )  ;    
					  }    
				  } 
			}catch(Exception e){
				e.printStackTrace();
				System.out.println("Ultimo for...");
			}
		  }
		
//	protected boolean traverse(int dir, int viewportWidth, int viewportHeight,  int[] visRect_inout) { 
//		switch(dir) {        
//		case Canvas.UP:       
//			return testaParaCima();         
//		case Canvas.LEFT:     
//			return testaParaCima();         
//		case Canvas.DOWN: 
//			return testaParaBaixo();         
//		case Canvas.RIGHT: 
//			return testaParaBaixo();         
//		case NONE:        // do nothing: just a form layout reflow         return true;      }        return false;   }  
//		}
//	}
	}
